#include "TrQuote.h"
#include "TrTraderSpi.h"

extern std::shared_ptr <spdlog::logger>	TrLog;

CTrQuote::CTrQuote() :
    m_runCount(0),
    bRunning(true)
{

}

void CTrQuote::CloseQuote()
{
    bRunning = false;
}

void CTrQuote::run()
{

    while (bRunning)
    {
        if(!m_pTraderSpi->m_bIsQuotting)
        {
            if(m_pTraderSpi->m_nLastUpdatePositionTime == 0 || m_pTraderSpi->m_nLastUpdateTradingAccount == 0)
            {
                m_pTraderSpi->reqQueryTradingAccount();
                QThread::msleep(1000);
                m_pTraderSpi->reqRspQryInvestorPosition();
                qDebug() << "CTrQuote::RspQryInvestorPosition()";
            }
            else
            {
                if(m_pTraderSpi->m_nLastInsertOrderTime > m_pTraderSpi->m_nLastUpdatePositionTime)
                {
                    m_pTraderSpi->reqRspQryInvestorPosition();
                }
                else
                {
                    //if(m_tTime.bIsDorationForSome(m_tTime.m_DefTradeTimeDuration))
                    {
                        m_pTraderSpi->reqQueryTradingAccount();
                        //qDebug() << "CTrQuote::run():reqQueryTradingAccount()";
                    }

                }
            }
        }

        QThread::msleep(1000);
        m_runCount++;
        if(m_runCount%10 == 0)
        {
            TrLog->debug("{:s}", __FUNCTION__);
        }
    }
    qDebug() << "Thread exit";
    exec();
}
